﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Serilog;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfNet8Test.XCDBTool;

namespace WpfNet8Test.Model
{
    public partial class CustomerViewModel: ObservableObject
    {
         
        private readonly XCSqliteTool _xcst;

        [ObservableProperty]
        private ObservableCollection<Customer> _customers;

        public CustomerViewModel()
        {
            _xcst = new XCSqliteTool();
            _xcst.InitializeDatabase();
            LoadCustomers();
        }

        private void LoadCustomers()
        {
            //Customers = new ObservableCollection<Customer>(_xcst.GetAllCustomers());

            //异步查询
            //Customers = new ObservableCollection<Customer>(_xcst.GetAllCustomersAsync().Result);

            //使用Dapper  比efcore简单易用
            Customers = new ObservableCollection<Customer>(_xcst.GetCustomersWithDapper());
        }

        [RelayCommand]
        private void AddCustomer()
        {
            if (Param1 == null || Param1 == "" || Param2 == null || Param2 == "") return;

            //_xcst.AddCustomer(new Customer { Name = Param1, Email = Param2 });

            //用事务新增记录
            _xcst.AddCustomerByTransaction(new Customer { Name = Param1, Email = Param2 });

            LoadCustomers();

            var ztool = new xiaochenLibrary.XCTool();
            var haha = ztool.MyStringContact(Param1, Param2);

            Log.Information("添加了一条记录 连接起来是{haha}",haha);
            Log.Information("添加了一条记录，内容是{name}:{email}",Param1,Param2);
        }

        [ObservableProperty]
        private string _param1;

        [ObservableProperty]
        private string _param2;

    }
}
